﻿exports.attachMenuItem = attachMenuItem;

const labelMenuItem = "Search in files...";

var ctxMenu = require("sdk/context-menu");
var ctxMenuItem = null;

function attachMenuItem(handler)
{
    if (null === ctxMenuItem)
    {
        ctxMenuItem = ctxMenu.Item({
            label: labelMenuItem,
            context: ctxMenu.SelectionContext(),
            contentScript:  'self.on("click", function () {' +
                            '  var text = window.getSelection().toString();' +
                            '  self.postMessage(text);' +
                            '});',
            onMessage: function (selectionText) {
                handler(selectionText);
            }
        });
    }   

    return ctxMenuItem;
}